<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
// use app\index\
class Systemt extends Base
{
    public function index() 
    {
        $mess=db("systemt")->where("id=1")->find();

        // print_r($mess);exit;
        $this->assign("mess",$mess);
		return view('index');
    }
    public function indexAddDone()
    {
		$updid=request()->param('updid'); 
        $data=$_POST;
        $data['aid']=$this->userMsg['id'];
        $res=db("systemt")->where("id",$updid)->update($data);
    
        // $where['status']=1;
        // $count=db("modelx")->where($where)->count();
        // $data=db("modelx")->where($where)->select();
        if($res){
            $mess['code']=200;
            $mess['msg']='保存成功';
        }else{
            $mess['code']=100;
            $mess['msg']='保存失败';
        }        
        return json_encode($mess);
    }
    public function category()
    {
		return view('category');
    }
    public function addCate()
    {
        $id=request()->param('id');        
        // print_r($id);exit;
        $where['status']=1;
        $where['pid']=0;        
        $category=db("category")->where($where)->select();
        if($id){
            $mess=db("category")->where("id",$id)->find();
            $this->assign("mess",$mess);
        }
        $this->assign("category",$category);
		return view('addCate');
    }
    public function getCate()
    {
        $category=db("category");
        $where['status']=1;
        $count=$category->where($where)->count();
        $data=$category->where($where)->order("pid asc , sort desc,id asc")->select();
        for ($i=0; $i < count($data); $i++) { 
            $data[$i]['pidName']=$category->where("id",$data[$i]['pid'])->value("name");
            if(!$data[$i]['pidName']){
                $data[$i]['pidName']='顶级分类';
            }
        }
        $mess['code']=0;
        $mess['msg']='';
        $mess['count']=$count;
        $mess['data']=$data;
        return json_encode($mess);
    }
    public function addCateDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;
        $data['aid']=$this->userMsg['id'];
        if($updid){
            $res=db("category")->where("id",$updid)->update($data);
        }else{
            $res=db("category")->insert($data);
        }        
        // $where['status']=1;
        // $count=db("modelx")->where($where)->count();
        // $data=db("modelx")->where($where)->select();
        if($res){
            $mess['code']=200;
            $mess['msg']='添加成功';
        }else{
            $mess['code']=100;
            $mess['msg']='添加失败';
        }        
        return json_encode($mess);
    }
    public function tagsadd()
    {
		return view('tagsadd');
    }
    public function channel()
    {
        $channel=db("channel");
        $modelx=db("modelx");
        // $where['status']=1;
        $count=$channel->where($where)->count();
        $data=$channel->where($where)->order("sort asc,id asc")->select();
        for ($i=0; $i < count($data); $i++) { 
            $data[$i]['pidName']=$channel->where("id",$data[$i]['pid'])->value("name");
            if(!$data[$i]['pidName']){
                $data[$i]['pidName']='顶级菜单';
            }
            $data[$i]['title']=$data[$i]['name'];
            $data[$i]['modelxName']=$modelx->where("id=".$data[$i]['modelx'])->value("name");            
            $data[$i]['tempName']=db("template")->where("id=".$data[$i]['template'])->value("name");            
        }
        $data=$this->makeTree($data);
        // print_r($data);exit;
        $this->assign("mess",$data);
        $this->assign("fname","栏目");
        return view('channel');
    }
    public function addChannel()
    {
        $id=request()->param('id');          
        // print_r($id);exit;
        $where['status']=1;
        $modelx=db("modelx")->where($where)->select();
        $where['pid']=0;        
        $channel=db("channel")->where($where)->select();
        for ($i=0; $i < count($channel); $i++) {             
            $channel[$i]['level']=db("modelx")->where("id=".$channel[$i]['modelx'])->value("level");
            $channel[$i]['type']=db("modelx")->where("id=".$channel[$i]['modelx'])->value("type");
            $channel[$i]['child']=db("channel")->where("pid=".$channel[$i]['id'])->select();
            for ($j=0; $j < count($channel[$i]['child']); $j++) { 
                $channel[$i]['child'][$j]['level']=db("modelx")->where("id=".$channel[$i]['child'][$j]['modelx'])->value("level");
                $channel[$i]['child'][$j]['type']=db("modelx")->where("id=".$channel[$i]['child'][$j]['modelx'])->value("type");
            }   
        }

        if($id){
            $mess=db("channel")->where("id",$id)->find();
            $temp=db("template")->where("modelx=".$mess['modelx'])->order("sort asc,id asc")->select();

            $this->assign("temp",$temp);
            $this->assign("mess",$mess);
        }
        // print_r($channel);exit;
        $this->assign("modelx",$modelx);
        $this->assign("channel",$channel);
        return view('addChannel');
    }
    public function addChannelDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;
        $type=db("modelx")->where("id",$data['modelx'])->value("type");
        if($type == 3 && $data['website'] == false){
            $mess['code']=300;
            $mess['msg']='你选择了URL模型，网址为必填项，请输入网址重试！';
            return json_encode($mess);
        }
        if(!$data['template']){
            $data['template']="index";
        }
        $data['aid']=$this->userMsg['id'];
        if($updid){
            if($data['pid'] == $updid){
                $mess['code']=300;
                $mess['msg']='不能选择自己为上级分类';
                return json_encode($mess);
            }
            $res=db("channel")->where("id",$updid)->update($data);
        }else{
            $res=db("channel")->insert($data);
        }        
        if($res){
            $mess['code']=200;
            $mess['msg']='添加成功';
        }else{
            $mess['code']=100;
            $mess['msg']='添加失败';
        } 
                      
        return json_encode($mess);
    }
    public function channel_del(){
        $id=request()->param('id'); 
        $data=db("channel")->where("status = 1 and pid=".$id)->select();
        if($data){
            $mess['code']=0;
            $mess['msg']="删除失败，有正在使用的子栏目";
            return json_encode($mess);
        }
        $res=db("channel")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }         
        return json_encode($mess);
    }
    public function channelSort(){
        $id=request()->param('id');            
        $sort=request()->param('sort');

        $data['sort']=$sort;
        $res=db("channel")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
    public function changeStatus(){
        $id=request()->param('id');            
        $status=request()->param('status');
        // return json_encode($_GET); 
        $data=db("channel")->where("status = 1 and pid=".$id)->select();
        if($data && $status == 0){
            $mess['code']=0;
            $mess['msg']="停用失败，该栏目下有已用子栏目";
            return json_encode($mess);
        }
        $data['status']=$status;
        $res=db("channel")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
    public function admint(){
        $admint=db("admin");
        $where['role']=0;
        $mess=$admint->where($where)->select();

        $this->assign("mess",$mess);
        $this->assign("fname","管理员");
        return view("admint");
    }public function addAdmint()
    {
        $id=request()->param('id');   

        if($id){
            $mess=db("admin")->where("id",$id)->find();
            $this->assign("mess",$mess);
        }
        return view('addAdmint');
    }
    public function addAdmintDone()
    {
        $updid=request()->param('updid'); 
        $username=request()->param('username'); 
        $password=request()->param('password'); 
        $passwd=request()->param('passwd'); 
        if($passwd !== $password){
            $mess['code']=100;
            $mess['msg']='两次输入的密码不一致';
            return json_encode($mess);
        }
        $data['username']=$username;
        $data['password']=md5($password);
        if($updid){
            $data['updatetime']=time();
            $res=db("admin")->where("id",$updid)->update($data);
        }else{
            $data['pid']=$this->userMsg['id'];
            $data['createtime']=time();
            $res=db("admin")->insert($data);
        }        
        if($res){
            $mess['code']=200;
            $mess['msg']='添加成功';
        }else{
            $mess['code']=100;
            $mess['msg']='添加失败';
        }             
        return json_encode($mess);
    }
    public function admin_del(){
        $id=$_GET['id'];
  
        $res=db("admin")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }         
        return json_encode($mess);
    }
    public function setMsg(){
        $userMsg=session("userMsg");
        $id=$userMsg['id'];

        $mess=db("admin")->where("id=".$id)->find();

        $this->assign("mess",$mess);
        return view("setMsg");
    }
    public function setmsgdone()
    {
        $id=request()->param('id'); 
        $passwd=request()->param('passwd'); 
        $password=request()->param('password'); 
        if($passwd !== $password){
            $mess['code']=100;
            $mess['msg']='两次输入的密码不一致';
            return json_encode($mess);
        }
        $data['username']=request()->param('username'); 
        $data['nickname']=request()->param('nickname'); 
        $data['thumb']=request()->param('thumb'); 
        $data['password']=md5($password);
        $data['updatetime']=time();
        $res=db("admin")->where("id",$id)->update($data);
     
        if($res){
            $mess['code']=200;
            $mess['msg']='修改成功';
        }else{
            $mess['code']=100;
            $mess['msg']='修改失败';
        }             
        return json_encode($mess);
    }
    public function modelx()
    {
        $modelx=db("modelx");
        $where['status']=1;
        // $count=$channel->where($where)->count();
        $mess=$modelx->where($where)->order("sort asc,id asc")->select();
       
        // $data=$this->makeTree($data);
        // print_r($data);exit;
        $this->assign("mess",$mess);
        $this->assign("fname","模型");
        return view('modelx');
    }
    public function template()
    {
        $modelx=db("template");
        $where['status']=1;
        // $count=$channel->where($where)->count();
        $mess=$modelx->where($where)->order("modelx asc ,sort asc,id asc")->select();
        for ($i=0; $i < count($mess); $i++) { 
            $mess[$i]['modelxName']=db("modelx")->where("id=".$mess[$i]['modelx'])->value("name");
        }
       
        // $data=$this->makeTree($data);
        // print_r($data);exit;
        $this->assign("mess",$mess);
        $this->assign("fname","模板");
        return view('template');
    }
    public function getTemp()
    {
        $modelx=request()->param('modelx');
        $template=db("template");
        $where['status']=1;
        $where['modelx']=$modelx;
        // $count=$channel->where($where)->count();
        $data=$template->where($where)->order("sort asc,id asc")->select();      
        // if($data){
        //     $mess['code']=1;
        //     $mess['msg']='ok';
        //     $mess['data']=$data;
        // }else{
        //     $mess['code']=0;
        //     $mess['msg']='该模型没有模板页';
        // }         
        return json_encode($data);
    }
    public function addTemplate()
    {
        $id=request()->param('id');          
        // print_r($id);exit;
        $where['status']=1;
        $modelx=db("modelx")->where($where)->select();
        $where['pid']=0;        
        

        if($id){
            $mess=db("template")->where("id",$id)->find();
            $temp=db("template")->where("modelx=".$mess['modelx'])->order("sort asc,id asc")->select();
            // if($mess['modelx']){
            //     $mess['modelxName']=db("modelx")->where("id=".$mess['modelx'])->value("name");  
            // }
            $this->assign("temp",$temp);
            $this->assign("mess",$mess);
        }
        // print_r($channel);exit;
        $this->assign("modelx",$modelx);
        return view('addTemplate');
    }
    public function addTemplateDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;
        $where['modelx']=$data['modelx'];
        $where['vpage']=$data['vpage'];
        $check=db("modelx")->where($where)->find();
        if($check){
            $mess['code']=300;
            $mess['msg']='同一模型下不能有相同的模板页';
            return json_encode($mess);
        }
        $data['aid']=$this->userMsg['id'];
        if($updid){
            $res=db("template")->where("id",$updid)->update($data);
        }else{
            $res=db("template")->insert($data);
        }        
        if($res){
            $mess['code']=200;
            $mess['msg']='保存成功';
        }else{
            $mess['code']=100;
            $mess['msg']='保存失败';
        } 
                      
        return json_encode($mess);
    }
    public function template_del(){
        $id=request()->param('id'); 
        $vpage=db("template")->where("id=".$id)->value("vpage");
        if($vpage == "index"){
            $mess['code']=3;
            $mess['msg']="默认模板页不可删除";
            return json_encode($mess);
        }
        $res=db("template")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }         
        return json_encode($mess);
    }
    public function templateSort(){
        $id=request()->param('id');            
        $sort=request()->param('sort');

        $data['sort']=$sort;
        $res=db("template")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
}
